package com.sun.talk.netty

import io.netty.channel.ChannelInitializer
import io.netty.channel.socket.SocketChannel
import io.netty.handler.codec.http.HttpObjectAggregator
import io.netty.handler.codec.http.HttpServerCodec
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler
import io.netty.handler.stream.ChunkedWriteHandler
import io.netty.handler.timeout.IdleStateHandler
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.stereotype.Component
import org.springframework.beans.factory.annotation.Autowired

class WebSocketServerInitializer : ChannelInitializer<SocketChannel>(){

    override fun initChannel(channel: SocketChannel) {
        // 通过管道添加handler
        channel.pipeline().apply {
            addLast(HttpServerCodec())
            addLast(ChunkedWriteHandler())// 写大数据流
            addLast(HttpObjectAggregator(1024 * 64)) // 将消息部分合并到FullHttpRequest和FullHttpResponse
            addLast(WebSocketServerProtocolHandler("/ws"))// WebSocket协议处理类
            addLast(TextWebSocketHandler())// WebSocket协议处理类
            addLast(IdleStateHandler(8,10,12))
            addLast(HeartBeatHandler())// 心跳处理类
        }
    }
}